package com.dianping.cat.configuration.business.transform;

import com.dianping.cat.configuration.business.IEntity;
import com.dianping.cat.configuration.business.IVisitor;
import com.dianping.cat.configuration.business.entity.BusinessItemConfig;
import com.dianping.cat.configuration.business.entity.BusinessReportConfig;
import com.dianping.cat.configuration.business.entity.CustomConfig;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

















public class DefaultXmlBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private int m_level;
  private StringBuilder m_sb;
  private boolean m_compact;

  public DefaultXmlBuilder() { this(false); }



/*  40 */   public DefaultXmlBuilder(boolean compact) { this(compact, new StringBuilder(4096)); }

  public DefaultXmlBuilder(boolean compact, StringBuilder sb) {
    this.m_visitor = this;
/*  44 */     this.m_compact = compact;
/*  45 */     this.m_sb = sb;
/*  46 */     this.m_sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n");
  }

  public String buildXml(IEntity<?> entity) {
/*  50 */     entity.accept(this.m_visitor);
/*  51 */     return this.m_sb.toString();
  }

  protected void endTag(String name) {
/*  55 */     this.m_level--;

/*  57 */     indent();
/*  58 */     this.m_sb.append("</").append(name).append(">\r\n");
  }


/*  62 */   protected String escape(Object value) { return escape(value, false); }


  protected String escape(Object value, boolean text) {
/*  66 */     if (value == null) {
/*  67 */       return null;
    }

/*  70 */     String str = toString(value);
/*  71 */     int len = str.length();
/*  72 */     StringBuilder sb = new StringBuilder(len + 16);

/*  74 */     for (int i = 0; i < len; i++) {
/*  75 */       char ch = str.charAt(i);

/*  77 */       switch (ch) {
        case '<':
/*  79 */           sb.append("&lt;");
          break;
        case '>':
/*  82 */           sb.append("&gt;");
          break;
        case '&':
/*  85 */           sb.append("&amp;");
          break;
        case '"':
/*  88 */           if (!text) {
/*  89 */             sb.append("&quot;");
            break;
          }
        default:
/*  93 */           sb.append(ch);
          break;
      }

    }
/*  98 */     return sb.toString();
  }

  protected void indent() {
/* 102 */     if (!this.m_compact) {
/* 103 */       for (int i = this.m_level - 1; i >= 0; i--) {
/* 104 */         this.m_sb.append("   ");
      }
    }
  }


/* 110 */   protected void startTag(String name) { startTag(name, false, null, new Object[0]); }



/* 114 */   protected void startTag(String name, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, closed, dynamicAttributes, nameValues); }



/* 118 */   protected void startTag(String name, Map<String, String> dynamicAttributes, Object... nameValues) { startTag(name, null, false, dynamicAttributes, nameValues); }


  protected void startTag(String name, Object text, boolean closed, Map<String, String> dynamicAttributes, Object... nameValues) {
/* 122 */     indent();

/* 124 */     this.m_sb.append('<').append(name);

/* 126 */     int len = nameValues.length;

/* 128 */     for (int i = 0; i + 1 < len; i += 2) {
/* 129 */       Object attrName = nameValues[i];
/* 130 */       Object attrValue = nameValues[i + 1];

/* 132 */       if (attrValue != null) {
/* 133 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 137 */     if (dynamicAttributes != null) {
/* 138 */       for (Map.Entry<String, String> e : dynamicAttributes.entrySet()) {
/* 139 */         this.m_sb.append(' ').append((String)e.getKey()).append("=\"").append(escape(e.getValue())).append('"');
      }
    }

/* 143 */     if (text != null && closed) {
/* 144 */       this.m_sb.append('>');
/* 145 */       this.m_sb.append(escape(text, true));
/* 146 */       this.m_sb.append("</").append(name).append(">\r\n");
    } else {
/* 148 */       if (closed) {
/* 149 */         this.m_sb.append('/');
      } else {
/* 151 */         this.m_level++;
      }

/* 154 */       this.m_sb.append(">\r\n");
    }
  }


  protected String toString(Object value) {
/* 160 */     if (value instanceof String)
/* 161 */       return (String)value; 
/* 162 */     if (value instanceof Collection) {
/* 163 */       Collection<Object> list = (Collection)value;
/* 164 */       StringBuilder sb = new StringBuilder(32);
/* 165 */       boolean first = true;

/* 167 */       for (Object item : list) {
/* 168 */         if (first) {
/* 169 */           first = false;
        } else {
/* 171 */           sb.append(',');
        }

/* 174 */         if (item != null) {
/* 175 */           sb.append(item);
        }
      }

/* 179 */       return sb.toString();
/* 180 */     }  if (value.getClass().isArray()) {
/* 181 */       int len = Array.getLength(value);
/* 182 */       StringBuilder sb = new StringBuilder(32);
/* 183 */       boolean first = true;

/* 185 */       for (int i = 0; i < len; i++) {
/* 186 */         Object item = Array.get(value, i);

/* 188 */         if (first) {
/* 189 */           first = false;
        } else {
/* 191 */           sb.append(',');
        }

/* 194 */         if (item != null) {
/* 195 */           sb.append(item);
        }
      }

/* 199 */       return sb.toString();
    }

/* 202 */     return String.valueOf(value);
  }

  protected void tagWithText(String name, String text, Object... nameValues) {
/* 206 */     if (text == null) {
      return;
    }

/* 210 */     indent();

/* 212 */     this.m_sb.append('<').append(name);

/* 214 */     int len = nameValues.length;

/* 216 */     for (int i = 0; i + 1 < len; i += 2) {
/* 217 */       Object attrName = nameValues[i];
/* 218 */       Object attrValue = nameValues[i + 1];

/* 220 */       if (attrValue != null) {
/* 221 */         this.m_sb.append(' ').append(attrName).append("=\"").append(escape(attrValue)).append('"');
      }
    }

/* 225 */     this.m_sb.append(">");
/* 226 */     this.m_sb.append(escape(text, true));
/* 227 */     this.m_sb.append("</").append(name).append(">\r\n");
  }

  protected void element(String name, String text, String defaultValue, boolean escape) {
/* 231 */     if (text == null || text.equals(defaultValue)) {
      return;
    }

/* 235 */     indent();

/* 237 */     this.m_sb.append('<').append(name).append(">");

/* 239 */     if (escape) {
/* 240 */       this.m_sb.append(escape(text, true));
    } else {
/* 242 */       this.m_sb.append("<![CDATA[").append(text).append("]]>");
    }

/* 245 */     this.m_sb.append("</").append(name).append(">\r\n");
  }



/* 250 */   public void visitBusinessItemConfig(BusinessItemConfig businessItemConfig) { startTag("business-item-config", true, null, new Object[] { "id", businessItemConfig.getId(), "view-order", Double.valueOf(businessItemConfig.getViewOrder()), "title", businessItemConfig.getTitle(), "show-count", Boolean.valueOf(businessItemConfig.isShowCount()), "show-avg", Boolean.valueOf(businessItemConfig.isShowAvg()), "show-sum", Boolean.valueOf(businessItemConfig.isShowSum()), "alarm", Boolean.valueOf(businessItemConfig.isAlarm()), "privilege", Boolean.valueOf(businessItemConfig.isPrivilege()) }); }



  public void visitBusinessReportConfig(BusinessReportConfig businessReportConfig) {
/* 255 */     startTag("business-report-config", null, new Object[] { "id", businessReportConfig.getId() });

/* 257 */     if (!businessReportConfig.getBusinessItemConfigs().isEmpty()) {
/* 258 */       for (BusinessItemConfig businessItemConfig : businessReportConfig.getBusinessItemConfigs().values()) {
/* 259 */         businessItemConfig.accept(this.m_visitor);
      }
    }

/* 263 */     if (!businessReportConfig.getCustomConfigs().isEmpty()) {
/* 264 */       for (CustomConfig customConfig : businessReportConfig.getCustomConfigs().values()) {
/* 265 */         customConfig.accept(this.m_visitor);
      }
    }

/* 269 */     endTag("business-report-config");
  }


  public void visitCustomConfig(CustomConfig customConfig) {
/* 274 */     startTag("custom-config", null, new Object[] { "id", customConfig.getId(), "view-order", Double.valueOf(customConfig.getViewOrder()), "title", customConfig.getTitle(), "alarm", Boolean.valueOf(customConfig.isAlarm()), "privilege", Boolean.valueOf(customConfig.isPrivilege()) });

/* 276 */     element("pattern", customConfig.getPattern(), null, true);

/* 278 */     endTag("custom-config");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-core-3.0.0.jar!/com/dianping/cat/configuration/business/transform/DefaultXmlBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */